<?php
namespace Admin\Controller;
use Think\Controller;
//猜明星
class IdiomController extends CommonController {

    public function index(){
        self::user_list();
    }

    //用户模块
    public function user_list()
    {
        $User = M('idiom_member'); // 实例化User对象

        $count      = $User->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $data['user_list'] = $User->field('idiom_member_id,nickName,gender,city,province,avatarUrl,logintime,opportunity,number,giveopportunity')->order('idiom_member_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);// 赋值分页输出
        $this->assign("data",$data);
        $this->display('user_list');
    }

    public function user_info()
    {


    }

    public function user_edit()
    {

    }

    //游戏规则
    public function game_rule()
    {
        $data['rule']   =   M('idiom_rule')->where(array('category'=>'idiom'))->find();
        $this->assign("data",$data);
        $this->display('game_rule');
    }

    public function game_rule_edit()
    {
        $where['idiom_rule_id'] =   I('post.idiom_rule_id','','htmlspecialchars');
        $data['shareadd']       =   I('post.shareadd','','htmlspecialchars');
        $data['maxshare']         =   I('post.maxshare','','htmlspecialchars');
        $data['giveopportunity']     =   I('post.giveopportunity','','htmlspecialchars');
        $data['sharelive']      =   I('post.sharelive','','htmlspecialchars');


        //安全判断

        $result     =   M('idiom_rule')->where($where)->save($data); // 根据条件更新记录

        //记录相关操作日志

        if (!empty($result)) 
        {
            $msg['state']   =   200;
            $msg['error']   =   '更新成功';
            $this->ajaxReturn ($msg,'JSON');
        }
        else
        {
            $msg['state']   =   -1;
            $msg['error']   =   '更新失败';
            $this->ajaxReturn ($msg,'JSON');
        }
    }

    //更多好玩
    public function game_more()
    {
        $data['game_list']  =   M('idiom_moreplay')->select();
        $this->assign("data",$data);
        $this->display('game_more');
    }

    public function game_more_add()
    {
        if (IS_AJAX) 
        {
            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('idiom_moreplay')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_more_add');
        }
    }

    public function game_more_edit()
    {
        if (IS_AJAX) 
        {
            $where['idiom_moreplay_id']  =   I('post.idiom_moreplay_id','','htmlspecialchars');
            if (empty($where['idiom_moreplay_id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');    
            }

            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envVersion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('idiom_moreplay')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['idiom_moreplay_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['idiom_moreplay_id']))
            {
                $this->error('非法请求');
            }

            $data['game_info']  =   M('idiom_moreplay')->where($where)->find();

            if ( empty($data)) 
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_more_edit');
        }
    }

    //游戏题库
    public function game_question()
    {

        $questions = M('idiom_questions');

        $count      = $questions->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出

        $data['game_question'] = $questions->field('sort,createtime',true)->order('idiom_questions_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);// 赋值分页输出

        $this->assign("data",$data);
        $this->display('game_question');
    }

    public function game_question_add()
    {
        exit;
        if (IS_AJAX) 
        {
            $data['answer']     =   I('post.answer','','htmlspecialchars');
            $data['category']   =   I('post.category','','htmlspecialchars');
            $data['imgsrc']     =   I('post.imgsrc','','htmlspecialchars');
            $data['sort']       =   I('post.sort','','htmlspecialchars');
            $data['puzzle1']    =   I('post.puzzle1','','htmlspecialchars');
            $data['puzzle2']    =   I('post.puzzle2','','htmlspecialchars');
            $data['puzzle3']    =   I('post.puzzle3','','htmlspecialchars');

            $result     =   M('star_question')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_question_add');
        }
    }

    public function game_question_edit()
    {
        exit;
        if (IS_AJAX) 
        {
            $where['star_question_id']  =   I('post.star_question_id','','htmlspecialchars');
            if (empty($where['star_question_id'])) 
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');    
            }

            $data['answer']     =   I('post.answer','','htmlspecialchars');
            $data['category']   =   I('post.category','','htmlspecialchars');
            $data['imgsrc']     =   I('post.imgsrc','','htmlspecialchars');
            $data['sort']       =   I('post.sort','','htmlspecialchars');
            $data['puzzle1']    =   I('post.puzzle1','','htmlspecialchars');
            $data['puzzle2']    =   I('post.puzzle2','','htmlspecialchars');
            $data['puzzle3']    =   I('post.puzzle3','','htmlspecialchars');

            $result     =   M('star_question')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['star_question_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['star_question_id'])) 
            {
                $this->error('非法请求');
            }

            $data['game_question']  =   M('star_question')->where($where)->find();

            if ( empty($data)) 
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_question_edit');
        }
    }

    public function game_weekrank_edit()
    {

        if (IS_AJAX) {
            $where['idiom_week_id'] = I('post.idiom_week_id', '', 'htmlspecialchars');
            if (empty($where['idiom_week_id'])) {
                $msg['state'] = -1;
                $msg['error'] = '非法请求';
                $this->ajaxReturn($msg, 'JSON');
            }

            $data['idiom_week_id'] = I('post.idiom_week_id', '', 'htmlspecialchars');
            $data['getnum'] = I('post.getnum', '', 'htmlspecialchars');


            $result = M('idiom_week')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) {
                $msg['state'] = 200;
                $msg['error'] = '更新成功';
                $this->ajaxReturn($msg, 'JSON');
            } else {
                $msg['state'] = -1;
                $msg['error'] = '更新失败';
                $this->ajaxReturn($msg, 'JSON');
            }
        } else {
            $where['idiom_week_id'] = I('get.id', '', 'htmlspecialchars');

            if (empty($where['idiom_week_id'])) {
                $this->error('非法请求');
            }

            $data['game_week'] = M('idiom_week')->where($where)->find();

            if (empty($data)) {
                $this->error('数据不存在');
            }

            $this->assign("data", $data);
            $this->display('game_weekrank_edit');
        }
    }
    //分享微信群
    public function wechat_group()
    {
        $data['wechat_group']  =   M()->table('mj_idiom_wechat_group w')->join('mj_idiom_member as uesr on  w.idiom_member_id = uesr.idiom_member_id')->field('w.*,uesr.nickName,uesr.avatarUrl')->order('w.idiom_wechat_group_id desc')->select();
        $this->assign("data",$data);
        $this->display('wechat_group');
    }

    //周排名
    public function week_ranking()
    {
        $year = date('Y');
        $week = (int)date('W');
        $info = M();

        $where = array(
            'week.year'=>$year,
            'week.week'=>$week,
        );

        $list =$info->table('mj_idiom_week as week')->join('mj_idiom_member as uesr on  week.idiom_member_id = uesr.idiom_member_id')->where($where)->field('week.getnum,week.idiom_week_id,week.year,week.week,week.appid,week.createtime,uesr.idiom_member_id,uesr.nickName,uesr.avatarUrl')->limit(100)->order('week.getnum desc' )->select();

        $this->assign("data",$list);
        $this->display('week_ranking');
    }

    public function lastweek_ranking(){

        $lastweek = (int)date("W", strtotime("-1 week"));;

        $week = date('W');

        $year = date('Y');
        if ($week=='1'){
            $year= date("Y", strtotime("-1 year"));
        }


        $info = M();

        $where = array(
            'week.year'=>$year,
            'week.week'=>$lastweek,
        );

        $list =$info->table('mj_idiom_week as week')->join('mj_idiom_member as uesr on  week.idiom_member_id = uesr.idiom_member_id')->where($where)->field('week.getnum,week.idiom_week_id,week.year,week.week,week.appid,week.createtime,uesr.idiom_member_id,uesr.nickName,uesr.avatarUrl')->limit(10)->order('week.getnum desc' )->select();

        if ($list) {
            $gift = M('idiom_gift')->field('giftname')->order('rank asc')->select();

            $list[0]['giftname'] = $gift[0]['giftname'];
            if ($list[1]['idiom_week_id']){
                $list[1]['giftname'] = $gift[1]['giftname'];
            }
            if ($list[2]['idiom_week_id']){
                $list[2]['giftname'] = $gift[2]['giftname'];
            }

            if ($list[3]['idiom_week_id']){
                $list[3]['giftname'] = $gift[3]['giftname'];
            }

            if ($list[4]['idiom_week_id']){
                $list[4]['giftname'] = $gift[3]['giftname'];
            }

            if ($list[5]['idiom_week_id']){
                $list[5]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[6]['idiom_week_id']){
                $list[6]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[7]['idiom_week_id']){
                $list[7]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[8]['idiom_week_id']){
                $list[8]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[9]['idiom_week_id']){
                $list[9]['giftname'] = $gift[4]['giftname'];
            }


        }
        $this->assign("data",$list);
        $this->display('lastweek_ranking');
    }

}